using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.IO;
using System.Web.UI.HtmlControls;
namespace Library.WebControls.SimpleSplitContainer {
	public class SpSplitContainerDesign : System.Web.UI.Design.ControlDesigner {
		public override string GetDesignTimeHtml() {
			SpSplitContainer spliter = (SpSplitContainer)Component;
			return GetDesignTimeHtml(spliter);
		}

		internal static string GetDesignTimeHtml(SpSplitContainer spliter) {
			StringWriter sw = new StringWriter();
			HtmlTextWriter html = new HtmlTextWriter(sw);
			switch (spliter.Direction) {
				case RepeatDirection.Horizontal:
					HorizoneRender(html, spliter);
					break;
				case RepeatDirection.Vertical:
					VerticalRender(html, spliter);
					break;
			}
			return sw.ToString();
		}

		private static void VerticalRender(HtmlTextWriter html, SpSplitContainer spliter) {
			SpSpliterTable mainTable = new SpSpliterTable();
			mainTable.Width = spliter.Width;		
			TableRow row = null;
			TableCell previewCell = null;


			if (spliter.Panels.Count > 0) {
				row = new SpSpliterRow();
				mainTable.Rows.Add(row);
				previewCell = new SpSpliterTableCell();
				previewCell.CssClass = "spSpliterPanel";

				previewCell.Controls.Add(spliter.Panels[0]);
				row.Cells.Add(previewCell);
			}
			for (int i = 1; i < spliter.Panels.Count; i++) {
			
				row = new SpSpliterRow();
			
				TableCell spliterCell = new SpSpliterTableCell();
				row.Cells.Add(spliterCell);
				mainTable.Rows.Add(row);
			

				spliterCell.CssClass = "spSpliter";
	
				spliterCell.Width = new Unit(2);
				row.Cells.Add(spliterCell);
				row = new SpSpliterRow();
				TableCell cell = new SpSpliterTableCell();
				cell.CssClass = "spSpliterPanel";

				cell.Controls.Add(spliter.Panels[i]);
				row.Cells.Add(cell);
				mainTable.Rows.Add(row);
			
				previewCell = cell;
			}

			mainTable.RenderControl(html);
		}
		private static void HorizoneRender(HtmlTextWriter html, SpSplitContainer spliter) {
			SpSpliterTable mainTable = new SpSpliterTable();			
			mainTable				.Width = spliter.Width;		
			TableRow row = new SpSpliterRow();
			TableCell previewCell =null;
			mainTable.Rows.Add(row);
			if (spliter.Panels.Count > 0) {
				previewCell = new SpSpliterTableCell();
				previewCell.CssClass = "spSpliterPanel";
				previewCell.Controls.Add(spliter.Panels[0]);
				row.Cells.Add(previewCell);
			}
			for (int i = 1; i < spliter.Panels.Count; i++) {
				TableCell spliterCell = new SpSpliterTableCell();
				spliterCell.CssClass = "spSpliter";			
				spliterCell.Width = new Unit(2);
				row.Cells.Add(spliterCell);		

				TableCell cell = new SpSpliterTableCell();
				cell.CssClass = "spSpliterPanel";

				cell.Controls.Add(spliter.Panels[i]);
				row.Cells.Add(cell);				
				previewCell = cell;
			}
			mainTable.RenderControl(html);
		}
	}
}
